package com.example.keli.ui.center;

import android.support.v4.view.ViewPager;
import android.widget.TextView;

import com.example.keli.R;
import com.example.keli.base.BaseFragment;
import com.example.keli.base.app.C;
import com.example.keli.base.utils.SPUtils;
import com.example.keli.ui.area.AreaActivity;
import com.example.keli.ui.area.AreaVO;
import com.example.keli.ui.home.adapter.FragmentAdapter;
import com.flyco.tablayout.SlidingTabLayout;
import com.flyco.tablayout.listener.OnTabSelectListener;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * description $desc$
 * created by jerry on 2019/3/18.
 */
public class CenterFragment extends BaseFragment implements OnTabSelectListener {

    @BindView(R.id.tabLayout)
    SlidingTabLayout slidingTabLayout;
    @BindView(R.id.viewPager)
    ViewPager viewPager;
    @BindView(R.id.tv_city)
    TextView tvCity;
    private FragmentAdapter mFragmentAdapter;

//    protected final List<String> sTitle = Arrays.asList("待启动", "报名中", "进行中", "已结束");
    protected final List<String> sTitle = Arrays.asList("进行中", "已结束");

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onChangedCity(AreaVO event){
        String regionName = event.getName();
        tvCity.setText(regionName+"站");
    }

    @Override
    protected int layoutId() {
        return R.layout.fragment_center;
    }

    @Override
    protected void bindData() {
        EventBus.getDefault().register(this);
//        mRegionName = SPUtils.get(getContext(), C.SP.AREA_CITY, "全国站");
        tvCity.setText(C.CURRENT_CITY_NAME);
        mFragmentAdapter = new FragmentAdapter(getChildFragmentManager());
        for (int i = 0; i < sTitle.size(); i++) {
            mFragmentAdapter.addFragment(ActiveFragment.newInstance(i + 3), sTitle.get(i));
        }
        viewPager.setAdapter(mFragmentAdapter);
        slidingTabLayout.setViewPager(viewPager);
        slidingTabLayout.setOnTabSelectListener(this);
        viewPager.setCurrentItem(0);

    }

    @Override
    protected void bindListener() {

    }

    @Override
    public void onTabSelect(int position) {

    }

    @Override
    public void onTabReselect(int position) {

    }

    @OnClick(R.id.ll_city)
    public void onViewClicked() {
        toActivity(AreaActivity.class);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
